/**
 * 编辑测试用例显示配置测试用例
 *
 * Created by liuxuechao on 2022/09/07.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/generate-tests/self_designed');

module.exports = describe('/PUT functions/:id/test-field-config.json', function () {
  const administrator = new Administrator();
  let project = {};
  const newUserIds = [];
  function url(functionId) {
    return `/functions/${functionId}/test-field-config.json`;
  }
  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'put');
  common.should.be.tester(uri, url, 'put');

  // 正常发起请求
  it(`it should update test-field-config of funtion in ${benchmark.name}`, function (done) {
    let projectVersion;
    let allFunctions;
    project.createRandomVersion()
      .then((res) => {
        projectVersion = res;
        return projectVersion.getAllFunctions(benchmark);
      })
      .then((res) => {
        allFunctions = res;
        return project.creator.login();
      })
      .then(() => {
        const { accessToken } = project.creator.token;
        chai.request(uri)
          .put(url(allFunctions[0].functionId))
          .set('Authorization', `Bearer ${accessToken}`)
          .send({ dataDepth: Math.ceil(Math.random() * 10) })
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.testFieldConfigMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
